package SM.BusinessLogic;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DBAccess {
	
	/*Static Functions*/
	
	
	/*Get default ConnectionString*/
	
	private static String GetConnectionString()
	{
		String conStr = "jdbc:mysql://localhost/SocietyManager?user=root&password=mihirj";
		
		return conStr;
	}
	
	/*Get Custom ConnectionString*/
	private static String GetConnectionString(String dataSource, String initialCatalog)
	{
		String conStr = "jdbc:mysql://" + dataSource + "/" + initialCatalog + "?user=root&password=mihirj";
		
		return conStr;
	}
	
	public static void CloseStatement(CallableStatement statement)
	{
		try {
	
			if(statement != null)
				statement.close();
			
			}catch(SQLException SQLEx)
			{
				System.out.println("Exception while closing statement");
			}
	}
	
	public static void CloseStatement(PreparedStatement statement)
	{
		try {
	
			if(statement != null)
				statement.close();
			
			}catch(SQLException SQLEx)
			{
				System.out.println("Exception while closing statement");
			}
	}
	
	/*Close connection*/
	
	public static void CloseConnection(Connection connection)
	{
		try {
	
			if(connection != null && !connection.isClosed())
				connection.close();
			
			}catch(SQLException SQLEx)
			{
				System.out.println("Exception while closing streams");
			}
	}
	
	/*Prepare Connection with Database*/
	public static Connection MakeConnection()
	{
		try
		{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			
			String conStr = GetConnectionString();
			
			Connection connection = DriverManager.getConnection(conStr);
			
			return connection;
			
		}catch(SQLException SQLEx) {
			 System.out.println("MySQL error: "+SQLEx.getErrorCode()+
			          " SQLSTATE:" +SQLEx.getSQLState());
			   System.out.println(SQLEx.getMessage());
		}
		catch(ClassNotFoundException ClassEx) {
			System.out.println("Oops! Class not found!!");
		}
		catch(IllegalAccessException IllEx) {
			System.out.println("Oops! Access is illegal!!");
		}
		catch(Exception Ex) {
			System.out.println("Some Further Exception Name : " + Ex.toString());
		}
		
		return null;
	}
	
	public static Connection MakeConnection(String dataSource, String initialCatalog)
	{
		try
		{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			
			String conStr = GetConnectionString(dataSource, initialCatalog);
			
			Connection connection = DriverManager.getConnection(conStr);
			
			return connection;
			
		}catch(SQLException SQLEx) {
			 System.out.println("MySQL error: "+SQLEx.getErrorCode()+
			          " SQLSTATE:" +SQLEx.getSQLState());
			   System.out.println(SQLEx.getMessage());
		}
		catch(ClassNotFoundException ClassEx) {
			System.out.println("Oops! Class not found!!");
		}
		catch(IllegalAccessException IllEx) {
			System.out.println("Oops! Access is illegal!!");
		}
		catch(Exception Ex) {
			System.out.println("Some Further Exception Name : " + Ex.toString());
		}
		
		return null;
	}
	
	
	public static CallableStatement GetStatementForProcedureCall(Connection connection, String procedureName)
	{
		try {
				CallableStatement statement = null;
				
				String callName = "{ call " + procedureName +" }";
		
				statement = connection.prepareCall(callName);
				
				return statement;
		}catch(SQLException SQLEx)
		{
			System.out.println("SQLException in GetStatementForProcedureCall \nCode : " + SQLEx.getErrorCode() + "\nMessage : " + SQLEx.getMessage() );
		}
		
		return null;
	}
}
